public class Solution {
    /**
     * @param nums: a list of integer
     * @param k: an integer
     * @return: return an integer, denote the number of continuous subarrays whose sum equals to k
     */
    Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
    public int subarraySumEqualsK (int[] nums, int k) {
        // write your code here
        for (int i = 1; i < nums.length; ++i) {
            nums[i] += nums[i - 1];
        }
        int ans = 0, tmp = 0;
        mp.put (0, 1);
        for (int i = 0; i < nums.length; ++i) {
            if (mp.containsKey (nums[i] - k)) ans += mp.get (nums[i] - k);
            tmp = mp.containsKey (nums[i]) ? mp.get (nums[i]) + 1 : 1;
            mp.put (nums[i], tmp);
        }
        return ans;
    }
}